package com.compass.doctor.ui.patient.viewmodel

import androidx.lifecycle.MutableLiveData
import com.compass.framework.ext.stringListToJson
import com.compass.framework.model.CaseHistory
import com.compass.framework.model.CreationInquiryCase
import com.compass.framework.model.HospitalizedLog
import com.compass.framework.model.InquiryCase
import com.compass.framework.model.InspectionRecord
import com.compass.framework.model.InspectionReport
import com.compass.framework.model.NewPatientWarp
import com.compass.framework.model.PatientISeeRecord
import com.compass.framework.model.PatientInfo
import com.compass.framework.model.PatientWap
import com.compass.framework.model.Tag
import com.compass.framework.model.TemplateDetails
import com.compass.framework.toast.TipsToast
import com.compass.framework.utils.DateUtils
import com.compass.network.callback.IApiErrorCallback
import com.compass.network.manager.ApiManager
import com.compass.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject

/**
 * Description： 患者viewModel
 * Date:2023/11/13 16:08
 */
class PatientViewModel : BaseViewModel() {

    val patientListLiveData: MutableLiveData<MutableList<PatientInfo>> = MutableLiveData()
    val myPatientLiveData: MutableLiveData<PatientWap?> = MutableLiveData()
    val patientDetailsLiveData = MutableLiveData<PatientInfo?>()
    val recordLiveData: MutableLiveData<MutableList<PatientISeeRecord>?> = MutableLiveData()
    val hospitalizedLogLiveData: MutableLiveData<MutableList<HospitalizedLog>?> = MutableLiveData()
    val inspectionRecordLiveData: MutableLiveData<MutableList<InspectionRecord>?> = MutableLiveData()
    val reportLiveData: MutableLiveData<MutableList<InspectionReport>?> = MutableLiveData()
    val allTagLiveData: MutableLiveData<MutableList<Tag>?> = MutableLiveData()
    val caseHistoryLiveData: MutableLiveData<MutableList<InquiryCase>?> = MutableLiveData()
    val addLiveData = MutableLiveData<Tag?>()
    val deleteLiveData = MutableLiveData<Long?>()
    val createSuccessLiveData = MutableLiveData<String?>()
    val newPatientLiveData = MutableLiveData<MutableList<NewPatientWarp>?>()
    val inquiryCaseLiveData = MutableLiveData<InquiryCase?>() //查询病历 和 病历详情
    val addSuccessLiveData = MutableLiveData<Any?>()
    val addSuccessForPhoneLiveData = MutableLiveData<Any?>()
    val cancelInspectionLiveData = MutableLiveData<Any?>()


    fun sendMessage(patientId: String?, text: String, msg: String?) {
        val params = JsonObject().apply {
            addProperty("patientId", patientId)
            addProperty("text", text)
            addProperty("consultationId", msg)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.sendMessage(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {

            }
        }, successBlock = {
        })
    }

    /**
     * 获取患者就诊记录
     * @param count 页码
     * @param pageSize 每页数量
     */
    fun getConsultationList(count: Int, pageSize: Int = 10, patientId: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getConsultationList(count, pageSize, patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                recordLiveData.value = null
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                recordLiveData.value = null
            } else {
                recordLiveData.value = it.list
            }
        })

    }

    /**
     * 取消检验检查
     */
    fun cancelInspection( id: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.cancelInspection(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                cancelInspectionLiveData.value = null
            }
        }, successBlock = {
            cancelInspectionLiveData.value=it
        })

    }


    val patientPrescriptionLiveData = MutableLiveData<MutableList<TemplateDetails>?>()//历史处方


    /**
     * 获取患者历史处方
     */
    fun getPatientPrescription(pageNo: Int = 1, patientId: String) {
        var statusValue: ArrayList<String> = ArrayList()
        statusValue.add("3")
        statusValue.add("2")
        statusValue.add("1")
        statusValue.add("-2")
        statusValue.add("-0")
        val params = JsonObject().apply {
            addProperty("pageNo", pageNo)
            addProperty("pageSize", 10)
            addProperty("timeType", 4)
            add("status", stringListToJson(statusValue))
            addProperty("ifTransfer", 0)
            addProperty("patientId", patientId)
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.getHisPrescription(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                patientPrescriptionLiveData.value = null
            }
        }) {
            if (it == null || it.list.isNullOrEmpty()) {
                patientPrescriptionLiveData.value = null
            } else {
                patientPrescriptionLiveData.value = it.list
            }
        }
    }


    /**
     * 创建病历
     */
    fun createCaseHistory(
        diseaseDescriptionText: String,
        patientId: String,
        list: ArrayList<String>?
    ) {
        val params = JsonObject().apply {
            addProperty("diseaseDescriptionText", diseaseDescriptionText)
            addProperty("patientId", patientId)
            add("diseaseDescription", stringListToJson(list))
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.createCaseHistory(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                createSuccessLiveData.value = null
            }
        }) {
            createSuccessLiveData.value = it
        }
    }


    /**
     * 举报
     */
    fun createReporting(
        content: String,
        list: ArrayList<String>?
    ) {
        val params = JsonObject().apply {
            addProperty("content", content)
            add("urlRecord", stringListToJson(list))
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.createReporting(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                createSuccessLiveData.value = null
            }
        }) {
            createSuccessLiveData.value = it
        }
    }


    /**
     * 获取患者列表带分页
     */
    fun getPatientList(mPage: Int, key: String?, tags: String? = "") {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientInfo(mPage, pageSize = 10, key, tags)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            if (it == null || it.list.isNullOrEmpty()) {
                patientListLiveData.value = null
            } else {
                patientListLiveData.value = it.list
            }

        })
    }


    /**
     * 获取患者列表带分页
     */
    fun getBriefSummary(patientId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getBriefSummary(patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                recordLiveData.value = null
            }
        }, successBlock = {
            recordLiveData.value = it

        })
    }

    /**
     *  获取患者病历列表-分页
     */
    fun getPatientCaseHistory(mPage: Int, patientId: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientCaseHistoryHis(patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                caseHistoryLiveData.value = null
            }
        }, successBlock = {

            caseHistoryLiveData.value = it
        })
    }


    /**
     *  获取患者病历详情
     */
    fun getPatientCaseHistoryDetailsHis(patientId: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientCaseHistoryDetailsHis(patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                inquiryCaseLiveData.value = null
            }
        }, successBlock = {
            inquiryCaseLiveData.value = it
        })
    }


    /**
     * 获取标签
     */
    fun getPatientTag() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientTag()
        }, errorCall = object : IApiErrorCallback {
        }, successBlock = {
            allTagLiveData.value = it
        })
    }


    /**
     * 获取病历-保存病历之前调用
     */
    fun getInquiryCaseHistory(patientId: String) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInquiryCaseHistory(patientId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                inquiryCaseLiveData.value=null
            }

        }, successBlock = {
            inquiryCaseLiveData.value=it
        })
    }


    /**
     * 添加问诊病历
     */
    fun addInquiryCase(inquiryCase: CreationInquiryCase) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.addInquiryCase(inquiryCase)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                addSuccessLiveData.value=null
            }
        }, successBlock = {
            addSuccessLiveData.value=it
        })
    }

    /**
     * 新增问诊病历--待办（针对于电话 和视频）
     */
    fun addInquiryCaseForPhone(inquiryCase: CreationInquiryCase) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.addInquiryCaseForPhone(inquiryCase)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                addSuccessForPhoneLiveData.value=null
            }
        }, successBlock = {
            addSuccessForPhoneLiveData.value=it
        })
    }

    /**
     * 添加标签
     */
    fun addPatientTag(patientId: String?, title: String) {
        val params = JsonObject().apply {
            addProperty("patientId", patientId)
            addProperty("title", title)
        }
        launchUIWithResult(responseBlock = {
            ApiManager.api.addPatientTag(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                addLiveData.value = null
            }
        }, successBlock = {
            it?.let {
                addLiveData.value = Tag(it, title)
            }

        })
    }

    /**
     * 删除标签
     */
    fun deletePatientTag(tagId: Long) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.deletePatientTag(tagId)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                deleteLiveData.value = null
            }
        }, successBlock = {
            it?.let {
                deleteLiveData.value = tagId
            }
        })
    }


    /**
     * 获取患者列表带分页
     */
    fun getMyPatientList(key: String?, tags: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getMyPatient(key, tags)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                myPatientLiveData.value = null
            }
        }, successBlock = {
            myPatientLiveData.value = it
        })
    }

    /**
     * 获取患者详情
     */
    fun getPatientDetails(id: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientDetails(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                patientDetailsLiveData.value = null
            }
        }, successBlock = {
            patientDetailsLiveData.value = it
        })
    }


    /**
     * 查询患者住院记录-列表
     */
    fun getPatientHospitalizedList(id: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientHospitalizedList(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                hospitalizedLogLiveData.value = null
            }
        }, successBlock = {
            hospitalizedLogLiveData.value = it
        })
    }

    /**
     * 查询患者住院记录-列表
     */
    fun getPatientInspectionList(id: String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getPatientInspectionList(id)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                inspectionRecordLiveData.value = null
            }
        }, successBlock = {
            inspectionRecordLiveData.value = it
        })
    }

    /**
     * 报告单
     */
    fun getInspectionReport(patientId: String?,reportType:String,type:Int,startDate:String?,endData:String?) {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getInspectionReport(patientId,reportType,type,startDate,endData)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                reportLiveData.value = null
            }
        }, successBlock = {
            reportLiveData.value = it
        })
    }


    /**
     * 新的患者-列表
     */
    fun getNewPatientList() {
        launchUIWithResult(responseBlock = {
            ApiManager.api.getNewPatientList()
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
                newPatientLiveData.value = null
            }
        }, successBlock = {
            newPatientLiveData.value = it
        })
    }

}